package com.limit.controller;

import com.limit.annotation.TokenParamAnnotation;
import com.limit.model.TestRequest;
import com.limit.token.TokensLimiter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

/**
 * controller测试
 * @author hbz
 */
@RestController
@Slf4j
@RequestMapping("tokens")
public class TokensTestController {


    @GetMapping("tokens_test")
    @TokenParamAnnotation(thread = 100, msg = "方法级注解限流，如果类也存在该注解，则以方法为准")
    public String tokensTest() throws NoSuchMethodException, ClassNotFoundException {
        Method method = this.getClass().getMethod("tokensTest");
        Class<? extends TokensTestController> aClass = this.getClass();
        String className = this.getClass().getName();
        TokensLimiter tokensLimiter = TokensLimiter.getInstance();

        Map<String, String> tokenParam = tokensLimiter.tokenParam;

        boolean methodAnnotationFlag = method.isAnnotationPresent(TokenParamAnnotation.class);
        if(methodAnnotationFlag){
            String[] value = tokenParam.get(className + "tokensTest").split("_");
            // 获取接口类型中的值
            TokenParamAnnotation annotation = method.getAnnotation(TokenParamAnnotation.class);
            return "调用成功，接口限流每秒访问接口次数：" + value[0] + "次，当前剩余令牌: " + value[1];
        }

        boolean classAnnotationFlag = aClass.isAnnotationPresent(TokenParamAnnotation.class);
        if(classAnnotationFlag){
            String[] value = tokenParam.get(className + "tokensTest").split("_");
            // 获取接口类型中的值
            TokenParamAnnotation annotation = aClass.getAnnotation(TokenParamAnnotation.class);
            return "调用成功，接口限流每秒访问接口次数：" + value[0] + "次，当前剩余令牌: " + value[1];
        }

        return "该接口不被限流";
    }

    @GetMapping("tokens_test2")
    @TokenParamAnnotation(thread = 30, msg = "方法级注解限流，如果类也存在该注解，则以方法为准")
    public String tokensTest2() throws NoSuchMethodException, ClassNotFoundException {
        Method method = this.getClass().getMethod("tokensTest2");
        Class<? extends TokensTestController> aClass = this.getClass();
        String className = this.getClass().getName();
        TokensLimiter tokensLimiter = TokensLimiter.getInstance();

        Map<String, String> tokenParam = tokensLimiter.tokenParam;

        boolean methodAnnotationFlag = method.isAnnotationPresent(TokenParamAnnotation.class);
        if(methodAnnotationFlag){
            String[] value = tokenParam.get(className + "tokensTest2").split("_");
            // 获取接口类型中的值
            TokenParamAnnotation annotation = method.getAnnotation(TokenParamAnnotation.class);
            return "调用成功，接口限流每秒访问接口次数：" + value[0] + "次，当前剩余令牌: " + value[1];
        }

        boolean classAnnotationFlag = aClass.isAnnotationPresent(TokenParamAnnotation.class);
        if(classAnnotationFlag){
            String[] value = tokenParam.get(className + "tokensTest2").split("_");
            // 获取接口类型中的值
            TokenParamAnnotation annotation = aClass.getAnnotation(TokenParamAnnotation.class);
            return "调用成功，接口限流每秒访问接口次数：" + value[0] + "次，当前剩余令牌: " + value[1];
        }

        return "该接口不被限流";
    }

    @GetMapping("tokens_test3")
    @TokenParamAnnotation(thread = 3, msg = "方法级注解限流，如果类也存在该注解，则以方法为准")
    public String tokensTest3() throws NoSuchMethodException, ClassNotFoundException {
        Method method = this.getClass().getMethod("tokensTest3");
        Class<? extends TokensTestController> aClass = this.getClass();
        String className = this.getClass().getName();
        TokensLimiter tokensLimiter = TokensLimiter.getInstance();

        Map<String, String> tokenParam = tokensLimiter.tokenParam;

        boolean methodAnnotationFlag = method.isAnnotationPresent(TokenParamAnnotation.class);
        if(methodAnnotationFlag){
            String[] value = tokenParam.get(className + "tokensTest3").split("_");
            // 获取接口类型中的值
            TokenParamAnnotation annotation = method.getAnnotation(TokenParamAnnotation.class);
            return "调用成功，接口限流每秒访问接口次数：" + value[0] + "次，当前剩余令牌: " + value[1];
        }

        boolean classAnnotationFlag = aClass.isAnnotationPresent(TokenParamAnnotation.class);
        if(classAnnotationFlag){
            String[] value = tokenParam.get(className + "tokensTest3").split("_");
            // 获取接口类型中的值
            TokenParamAnnotation annotation = aClass.getAnnotation(TokenParamAnnotation.class);
            return "调用成功，接口限流每秒访问接口次数：" + value[0] + "次，当前剩余令牌: " + value[1];
        }

        return "该接口不被限流";
    }

    @GetMapping("tokens_test4")
    public String tokensTest4() throws NoSuchMethodException, ClassNotFoundException {
        Method method = this.getClass().getMethod("tokensTest4");
        Class<? extends TokensTestController> aClass = this.getClass();
        String className = this.getClass().getName();
        TokensLimiter tokensLimiter = TokensLimiter.getInstance();

        Map<String, String> tokenParam = tokensLimiter.tokenParam;

        boolean methodAnnotationFlag = method.isAnnotationPresent(TokenParamAnnotation.class);
        if(methodAnnotationFlag){
            String[] value = tokenParam.get(className + "tokensTest4").split("_");
            // 获取接口类型中的值
            TokenParamAnnotation annotation = method.getAnnotation(TokenParamAnnotation.class);
            return "调用成功，接口限流每秒访问接口次数：" + value[0] + "次，当前剩余令牌: " + value[1];
        }

        boolean classAnnotationFlag = aClass.isAnnotationPresent(TokenParamAnnotation.class);
        if(classAnnotationFlag){
            String[] value = tokenParam.get(className + "tokensTest4").split("_");
            // 获取接口类型中的值
            TokenParamAnnotation annotation = aClass.getAnnotation(TokenParamAnnotation.class);
            return "调用成功，接口限流每秒访问接口次数：" + value[0] + "次，当前剩余令牌: " + value[1];
        }

        return "该接口不被限流";
    }

    @PostMapping("tokens_test5")
    @TokenParamAnnotation(thread = 8, msg = "带参测试")
    public String tokensTest5(@RequestBody List<TestRequest> testRequest, HttpServletRequest httpServletRequest) {


        return "该接口不被限流";
    }

    @GetMapping("tokens_test6")
    @TokenParamAnnotation(thread = 8, msg = "带参测试")
    public String tokensTest6(@RequestParam(value = "a") int a , @RequestParam(value = "b") String b){


        return "该接口不被限流";
    }
}
